package com.mammoth.Bodybuilding.controller.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mammoth.Bodybuilding.entity.ResultObj;
import com.mammoth.Bodybuilding.entity.SysMenu;
import com.mammoth.Bodybuilding.entity.SysUserObj;
import com.mammoth.Bodybuilding.service.IMenuService;
import com.mammoth.Bodybuilding.util.CurrencySaas;
import com.mammoth.Bodybuilding.util.RoleType;

import net.sf.json.JSONObject;

/**
 * 
 * @title : MenuController
 * @description : 菜单控制器
 * @company : com.mammoth.Bodybuilding.controller.web
 * @project Mammoth
 * @author xingz
 * @date 2018年5月9日 下午9:00:20
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController {

	/** 注入用户service **/
	// @Autowired
	// private IUserService userService;

	/** 注入用户service **/
	// @Autowired
	// private IRoleService roleService;

	/** 注入菜单数据仓库 **/
	@Autowired
	private IMenuService menuService;

	/**
	 * 跳转菜单页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping(value = "allmenu")
	public String allMenu(Model model) {
		/** 获取用户信息 **/
		SysUserObj user = (SysUserObj) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<RoleType> roles = new ArrayList<>();
		roles.add(RoleType.ROLE_PROGRAM);
		JSONObject flag = CurrencySaas.checkUserRole(user, roles);
		if (flag.getBoolean("flag")) {
			/** 菜单开启状态 **/
			SysMenu menu = menuService.getMenuByPath("/menu/allmenu");
			model.addAttribute("pageid", menu.getFid());
			model.addAttribute("page", menu.getMenuPath());
			return "menulist";
		} else {
			return "error";
		}
	}

	/**
	 * 获取所有父级菜单
	 * 
	 * @return
	 */
	@PostMapping(value = "/getallfmenu")
	@ResponseBody
	public String getAllFMenu() {
		List<SysMenu> fMenu = menuService.getFMenu();
		JSONObject json = new JSONObject();
		json.put("res", fMenu);
		json.put("total", 0);
		json.put("flag", true);
		json.put("msg", "查询成功");
		return json.toString();
	}

	/**
	 * 获取所有菜单
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param name
	 * @param path
	 * @return
	 */
	@PostMapping(value = "/getallmenu")
	@ResponseBody
	public String getAllMenu(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam String name,
			@RequestParam String path, @RequestParam(defaultValue = "0") Long fid) {
		JSONObject result = new JSONObject();
		/** 获取用户信息 **/
		SysUserObj user = (SysUserObj) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<RoleType> roles = new ArrayList<>();
		roles.add(RoleType.ROLE_PROGRAM);
		JSONObject flag = CurrencySaas.checkUserRole(user, roles);
		if (flag.getBoolean("flag")) {
			ResultObj menu = menuService.getAllMenu(pageSize, pageIndex, name, path, fid);
			result.put("res", menu.getResult());
			result.put("total", menu.getTotal());
			result.put("flag", menu.isFlag());
			result.put("msg", menu.getMessage());
		} else {
			result.put("flag", false);
			result.put("msg", "当前用户没有权限");
		}
		return result.toString();
	}

	/**
	 * 获取fu获取菜单名称
	 * 
	 * @param fid
	 * @return
	 */
	@PostMapping(value = "/getbyfid")
	@ResponseBody
	public String getByFid(@RequestParam Long fid) {
		return menuService.getByFid(fid);
	}

	/**
	 * 保存菜单
	 * 
	 * @param menuName
	 *            菜单名称
	 * @param menuPath
	 *            菜单路径
	 * @param menuIcon
	 *            菜单icon
	 * @param fMenu
	 *            父菜单
	 * @param role
	 *            权限
	 * @param sore
	 *            排序
	 * @return
	 */
	@PostMapping(value = "/savemenu")
	@ResponseBody
	public String saveMenu(@RequestParam String menuName, @RequestParam String menuPath, @RequestParam String menuIcon,
			@RequestParam Long fMenu, @RequestParam(value = "role") List<Long> role, @RequestParam int sore) {
		JSONObject result = new JSONObject();
		/** 获取用户信息 **/
		SysUserObj user = (SysUserObj) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<RoleType> roles = new ArrayList<>();
		roles.add(RoleType.ROLE_PROGRAM);
		JSONObject flag = CurrencySaas.checkUserRole(user, roles);
		if (flag.getBoolean("flag")) {
			ResultObj menu = menuService.saveMenu(menuName, menuPath, menuIcon, fMenu, role, sore);
			result.put("flag", menu.isFlag());
			result.put("msg", menu.getMessage());
		} else {
			result.put("flag", false);
			result.put("msg", "当前用户没有权限");
		}
		return result.toString();
	}

	/**
	 * 根据id修改菜单信息
	 * 
	 * @param id
	 * @param menuName
	 * @param menuPath
	 * @param menuIcon
	 * @param fMenu
	 * @param role
	 * @param sore
	 * @return
	 */
	@PostMapping(value = "/updatemenu")
	@ResponseBody
	public String updatemenu(@RequestParam Long id, @RequestParam String menuName, @RequestParam String menuPath,
			@RequestParam String menuIcon, @RequestParam Long fMenu, @RequestParam(value = "edit_role") List<Long> edit_role,
			@RequestParam int sore) {
		JSONObject result = new JSONObject();
		/** 获取用户信息 **/
		SysUserObj user = (SysUserObj) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<RoleType> roles = new ArrayList<>();
		roles.add(RoleType.ROLE_PROGRAM);
		JSONObject flag = CurrencySaas.checkUserRole(user, roles);
		if (flag.getBoolean("flag")) {
			ResultObj menu = menuService.updateMenu(id, menuName, menuPath, menuIcon, fMenu, edit_role, sore);
			result.put("flag", menu.isFlag());
			result.put("msg", menu.getMessage());
		} else {
			result.put("flag", false);
			result.put("msg", "当前用户没有权限");
		}
		return result.toString();
	}

	/**
	 * 根据id删除菜单信息
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping(value = "/delmenu")
	@ResponseBody
	public String delmenu(@RequestParam("ids[]") List<Long> ids) {
		JSONObject result = new JSONObject();
		/** 获取用户信息 **/
		SysUserObj user = (SysUserObj) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<RoleType> roles = new ArrayList<>();
		roles.add(RoleType.ROLE_PROGRAM);
		JSONObject flag = CurrencySaas.checkUserRole(user, roles);
		if (flag.getBoolean("flag")) {
			ResultObj menu = menuService.delMenu(ids);
			result.put("flag", menu.isFlag());
			result.put("msg", menu.getMessage());
		} else {
			result.put("flag", false);
			result.put("msg", "当前用户没有权限");
		}
		return result.toString();
	}

}
